<?php
//  ------------------------------------------------------------------------ //
//                          SHORTCUTS FOR XOOPS 2                            //
//                  Copyright (c) 2005-2006 Instant Zero                     //
//                     <http://xoops.instant-zero.com/>                      //
// ------------------------------------------------------------------------- //
//  This program is free software; you can redistribute it and/or modify     //
//  it under the terms of the GNU General Public License as published by     //
//  the Free Software Foundation; either version 2 of the License, or        //
//  (at your option) any later version.                                      //
//                                                                           //
//  You may not change or alter any portion of this comment or credits       //
//  of supporting developers from this source code or any supporting         //
//  source code which is considered copyrighted (c) material of the          //
//  original comment or credit authors.                                      //
//                                                                           //
//  This program is distributed in the hope that it will be useful,          //
//  but WITHOUT ANY WARRANTY; without even the implied warranty of           //
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            //
//  GNU General Public License for more details.                             //
//                                                                           //
//  You should have received a copy of the GNU General Public License        //
//  along with this program; if not, write to the Free Software              //
//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA //
//  ------------------------------------------------------------------------ //

include_once('header.php');
include_once(XOOPS_ROOT_PATH.'/header.php');
include_once XOOPS_ROOT_PATH.'/modules/shortcuts/include/functions.php';
$myts =& MyTextSanitizer::getInstance();

if(!is_object($xoopsUser)) {	// Only for registred users
	redirect_header(XOOPS_URL.'/index.php', 2, _SHORTCUTS_REGISTRED);
}

$xoopsOption['template_main'] = 'shortcuts_manage.html';

$maxshortcuts = st_getmoduleoption('maxshortcuts');
$titlelimit = st_getmoduleoption('titlelimit');
$enablerating = st_getmoduleoption('enablerating');
$savehits = st_getmoduleoption('savehits');

$xoopsTpl->assign('savehits',$savehits);
$xoopsTpl->assign('enable_rating',$enablerating);

$shortcuts_handler =& xoops_getmodulehandler('shortcuts', 'shortcuts');

if($titlelimit>0) {
	$xoopsTpl->assign('maxlength','maxlength='.$titlelimit);
} else {
	$xoopsTpl->assign('maxlength','');
}
$xoopsTpl->assign('xoops_pagetitle',_SHORTCUTS_MANAGEMENTS);
$xoopsTpl->assign('shortcuts_select_options',array('0'=>'--', '10'=>10, '9'=>9, '8'=>8, '7'=>7, '6'=>6, '5'=>5, '4'=>4, '3'=>3, '2'=>2, '1'=>1));

$op = '';
$op = isset($_GET['Op']) ? $_GET['Op'] : '';
if($op == '') {
	$op = isset($_POST['Op']) ? $_POST['Op'] : '';
}

if($op =='') {
	$op = 'list';
}

if(isset($_POST['modify']) || isset($_POST['modify_x']) || isset($_POST['modify_y']) ) {
	$op = 'Modify';
} elseif(isset($_POST['delete']) || isset($_PST['delete_x']) || isset($_POST['delete_y']) ) {
	$op = 'Delete';
}

$uid = $xoopsUser->getVar('uid');

function ListShortcuts()
{
	global $shortcuts_handler, $uid, $xoopsTpl, $enablerating;
	$shortcutarr = array();
	$critere = new Criteria('uid', $uid,'=');
	$shortcutarr = $shortcuts_handler->getObjects($critere);
   	foreach ($shortcutarr as $one_shortcut) {
   		$xoopsTpl->append('shortcuts_list', array(	'date' => formatTimestamp($one_shortcut->getVar('date')),
   													'title' => $one_shortcut->getVar('title'),
   													'url' => $one_shortcut->getVar('url'),
   													'hits' => $one_shortcut->getVar('hits'),
   													'rating' => $one_shortcut->getVar('rating'),
   													'id' => $one_shortcut->getVar('shortcutid')));
	}
}


switch($op) {
	case 'Modify':
		$shortcut = $shortcuts_handler->get(intval($_POST['shortcutid']));
		$shortcut->unsetNew();
		$shortcut->setVar('title',$_POST['title']);
		if ($enablerating) {
			$shortcut->setVar('rating',intval($_POST['Rating']));
		}
		$shortcuts_handler->insert($shortcut);
		ListShortcuts();
		break;

	case 'Delete':
		$shortcut = $shortcuts_handler->get(intval($_POST['shortcutid']));
		$shortcuts_handler->delete($shortcut);
		ListShortcuts();
		break;

	case 'list':
		ListShortcuts();
		break;
}
include_once(XOOPS_ROOT_PATH.'/footer.php');
?>